# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  def odd(id)
    if( id%2 == 0)
      return "even"
    elsif( id%2 > 0)
      return "odd"
    end
  end
  
  def popup(screen, popup)
    link = "#"
    if (screen.nil? || popup.nil?)
      out = "erreur"
    else
      out = '<a href="#" class="tip">'+ (screen.to_s) + '<span>'+ (popup.to_s) +'</span></a>'
    end
    return out
  end

  #retourne une valeur tronquée d'un int
  # TODO : "precision" devrait etre defini par l'utilisateur dans les parametres de l'appli
  # TODO : number_with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
  def get_value_or_blank(var, value)
    val = var[value.to_sym]
    return (pretty(val))
  end

  # marche pas
  # retourne 0.0 si valeur nulle
  # (pourquoi?)
  def pretty2(val)
    precision = 1
    out = "" if (val.equal?(0))
    out = "" if (val == 0.0)
    out = val.to_i if (val == val.to_i)
    out = val if precision == 0
    out = (val * 10 ** precision).round.to_f / 10 ** precision
    out
  end
  
  def pretty(val)
    precision = 1
    return "" if (val.equal?(0))
    return "" if (val == 0.0)
    return val.to_i if (val == val.to_i)
    return val if precision == 0
    return (val * 10 ** precision).round.to_f / 10 ** precision
  end
  
  
  # h_to_s : affiche correctement un Hash en string  
  def h_to_s(parcelle)
    out = "debut<br>"
    if(parcelle.class.equal?(Hash))
      parcelle.each do |key, value|
        out+= "#{key} => {"
        out+= print_value(value, 1)
        out+= "<br>"
      end
    else
      out+= parcelle
    end
    out+= "<br>"
    return out
  end
  
  def print_value(val, n)
    dd = "<dd>"
    out=""
    if(val.class.equal?(Hash))
     out+=""
     val.each do |key, value|
        n.times {out += dd}
        out += "#{key} => "
        (n+1).times {out += dd}
        out += "{"
        out += print_value3(value)
        #out += value.to_s
        
        (n+1).times {out += dd}
        out += "}"
      end
      n.times {out += dd}
    else
      out += "#{val}"
    end
    out += ""
  end

  def print_value3(val)
    out=""
    if(val.class.equal?(Hash))
      val.each do |key, value|
        out+= "<dd><dd>"
        out += "#{key} => #{value}"
      end
    else
      out += "#{val.to_s}"
    end
    out+= ""
  end
  
    
end